package jsqlite.JDBC2z;

import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
import jsqlite.Exception;
import jsqlite.TableResult;

/* loaded from: classes2.dex */
public class JDBCStatement implements Statement {
    protected JDBCConnection conn;
    protected int maxrows = 0;
    protected int updcnt = 0;
    protected JDBCResultSet rs = null;
    private ArrayList<String> batch = null;

    @Override // java.sql.Statement
    public void clearWarnings() throws SQLException {
    }

    @Override // java.sql.Statement
    public int getFetchDirection() throws SQLException {
        return 1002;
    }

    @Override // java.sql.Statement
    public int getFetchSize() throws SQLException {
        return 1;
    }

    @Override // java.sql.Statement
    public int getMaxFieldSize() throws SQLException {
        return 0;
    }

    @Override // java.sql.Statement
    public int getResultSetConcurrency() throws SQLException {
        return 1007;
    }

    @Override // java.sql.Statement
    public int getResultSetHoldability() throws SQLException {
        return 1;
    }

    @Override // java.sql.Statement
    public int getResultSetType() throws SQLException {
        return 1004;
    }

    @Override // java.sql.Statement
    public SQLWarning getWarnings() throws SQLException {
        return null;
    }

    @Override // java.sql.Statement
    public boolean isPoolable() throws SQLException {
        return false;
    }

    @Override // java.sql.Wrapper
    public boolean isWrapperFor(Class cls) throws SQLException {
        return false;
    }

    public JDBCStatement(JDBCConnection jDBCConnection) {
        this.conn = jDBCConnection;
    }

    @Override // java.sql.Statement
    public void setFetchSize(int i) throws SQLException {
        if (i != 1) {
            throw new SQLException("fetch size not 1");
        }
    }

    @Override // java.sql.Statement
    public int getMaxRows() throws SQLException {
        return this.maxrows;
    }

    @Override // java.sql.Statement
    public void setMaxRows(int i) throws SQLException {
        if (i < 0) {
            throw new SQLException("max must be >= 0 (was " + i + ")");
        }
        this.maxrows = i;
    }

    @Override // java.sql.Statement
    public void setFetchDirection(int i) throws SQLException {
        throw new SQLException("not supported");
    }

    @Override // java.sql.Statement
    public void setQueryTimeout(int i) throws SQLException {
        if (isClosed()) {
            throw new SQLException("can't set a query timeout on a closed statement");
        }
        if (i < 0) {
            throw new SQLException("can't set a query timeout of less than 0 seconds");
        }
        if (i == 0) {
            this.conn.timeout = 5000;
            return;
        }
        this.conn.timeout = i * 1000;
    }

    @Override // java.sql.Statement
    public int getQueryTimeout() throws SQLException {
        return this.conn.timeout / 1000;
    }

    @Override // java.sql.Statement
    public ResultSet getResultSet() throws SQLException {
        return this.rs;
    }

    public ResultSet executeQuery(String str, String[] strArr, boolean z) throws SQLException {
        JDBCResultSet jDBCResultSet = this.rs;
        if (jDBCResultSet != null) {
            jDBCResultSet.close();
            this.rs = null;
        }
        this.updcnt = -1;
        JDBCConnection jDBCConnection = this.conn;
        if (jDBCConnection == null || jDBCConnection.db == null) {
            throw new SQLException("stale connection");
        }
        boolean z2 = (this.conn.autocommit || this.conn.intrans) ? false : true;
        TableResult tableResult = null;
        int i = 0;
        while (true) {
            if (z2) {
                try {
                    this.conn.db.exec("BEGIN TRANSACTION", null);
                    this.conn.intrans = true;
                } catch (Exception e) {
                    if (!this.conn.db.is3() || this.conn.db.last_error() != 5) {
                        break;
                    }
                    JDBCConnection jDBCConnection2 = this.conn;
                    i++;
                    if (!jDBCConnection2.busy3(jDBCConnection2.db, i)) {
                        break;
                    }
                    if (z2) {
                        try {
                            if (this.conn.intrans) {
                                this.conn.db.exec("ROLLBACK", null);
                                this.conn.intrans = false;
                            }
                        } catch (Exception unused) {
                        }
                    }
                    int i2 = (i * 10) + 20;
                    if (i2 > 1000) {
                        i2 = 1000;
                    }
                    synchronized (this) {
                        wait(i2);
                    }
                    throw new SQLException(e.toString());
                }
            }
            if (strArr == null) {
                if (z) {
                    this.conn.db.exec(str, null);
                } else {
                    tableResult = this.conn.db.get_table(str, this.maxrows);
                }
            } else if (z) {
                this.conn.db.exec(str, null, strArr);
            } else {
                tableResult = this.conn.db.get_table(str, this.maxrows, strArr);
            }
            this.updcnt = (int) this.conn.db.changes();
            if (!z && tableResult == null) {
                throw new SQLException("no result set produced");
            }
            if (!z && tableResult != null) {
                this.rs = new JDBCResultSet(new TableResultX(tableResult), this);
            }
            return this.rs;
        }
        throw new SQLException(e.toString());
    }

    @Override // java.sql.Statement
    public ResultSet executeQuery(String str) throws SQLException {
        return executeQuery(str, null, false);
    }

    @Override // java.sql.Statement
    public boolean execute(String str) throws SQLException {
        return executeQuery(str) != null;
    }

    @Override // java.sql.Statement
    public void cancel() throws SQLException {
        JDBCConnection jDBCConnection = this.conn;
        if (jDBCConnection == null || jDBCConnection.db == null) {
            throw new SQLException("stale connection");
        }
        this.conn.db.interrupt();
    }

    @Override // java.sql.Statement
    public Connection getConnection() throws SQLException {
        return this.conn;
    }

    @Override // java.sql.Statement
    public void addBatch(String str) throws SQLException {
        if (this.batch == null) {
            this.batch = new ArrayList<>(1);
        }
        this.batch.add(str);
    }

    @Override // java.sql.Statement
    public int[] executeBatch() throws SQLException {
        ArrayList<String> arrayList = this.batch;
        if (arrayList == null) {
            return new int[0];
        }
        int size = arrayList.size();
        int[] iArr = new int[size];
        for (int i = 0; i < size; i++) {
            iArr[i] = -3;
        }
        int i2 = 0;
        for (int i3 = 0; i3 < size; i3++) {
            try {
                execute(this.batch.get(i3));
                iArr[i3] = this.updcnt;
            } catch (SQLException unused) {
                i2++;
            }
        }
        if (i2 <= 0) {
            return iArr;
        }
        throw new BatchUpdateException("batch failed", iArr);
    }

    @Override // java.sql.Statement
    public void clearBatch() throws SQLException {
        ArrayList<String> arrayList = this.batch;
        if (arrayList != null) {
            arrayList.clear();
            this.batch = null;
        }
    }

    @Override // java.sql.Statement, java.lang.AutoCloseable
    public void close() throws SQLException {
        clearBatch();
        this.conn = null;
    }

    @Override // java.sql.Statement
    public int executeUpdate(String str) throws SQLException {
        executeQuery(str, null, true);
        return this.updcnt;
    }

    @Override // java.sql.Statement
    public boolean getMoreResults() throws SQLException {
        JDBCResultSet jDBCResultSet = this.rs;
        if (jDBCResultSet != null) {
            jDBCResultSet.close();
            this.rs = null;
            return false;
        }
        return false;
    }

    @Override // java.sql.Statement
    public int getUpdateCount() throws SQLException {
        return this.updcnt;
    }

    @Override // java.sql.Statement
    public void setCursorName(String str) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public void setEscapeProcessing(boolean z) throws SQLException {
        throw new SQLException("not supported");
    }

    @Override // java.sql.Statement
    public void setMaxFieldSize(int i) throws SQLException {
        throw new SQLException("not supported");
    }

    @Override // java.sql.Statement
    public boolean getMoreResults(int i) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public ResultSet getGeneratedKeys() throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public int executeUpdate(String str, int i) throws SQLException {
        if (i != 2) {
            throw new SQLFeatureNotSupportedException("generated keys not supported");
        }
        return executeUpdate(str);
    }

    @Override // java.sql.Statement
    public int executeUpdate(String str, int[] iArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public int executeUpdate(String str, String[] strArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public boolean execute(String str, int i) throws SQLException {
        if (i != 2) {
            throw new SQLFeatureNotSupportedException("autogenerated keys not supported");
        }
        return execute(str);
    }

    @Override // java.sql.Statement
    public boolean execute(String str, int[] iArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public boolean execute(String str, String[] strArr) throws SQLException {
        throw new SQLFeatureNotSupportedException();
    }

    @Override // java.sql.Statement
    public boolean isClosed() throws SQLException {
        return this.conn == null;
    }

    @Override // java.sql.Statement
    public void setPoolable(boolean z) throws SQLException {
        if (z) {
            throw new SQLException("poolable statements not supported");
        }
    }

    @Override // java.sql.Wrapper
    public <T> T unwrap(Class<T> cls) throws SQLException {
        throw new SQLException("unsupported");
    }
}
